﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;

namespace Garlic.DAL
{
    public class StTerminalDAL
    {
        private const string selectAllTerminal = @"select * from web_vm_customer t where code=7";

        private const string selectAll_REF_CODE = @" select * from st_ref_codes {0}";

        private const string selectAllPorts = @" select pot_id , pot_portcd, pot_portlocnnm,pot_portengnm  from st_ports {0}";



        /// <summary>
        /// Get DataTable
        /// </summary>
        /// <param name="textWhere">conditon</param>
        /// <param name="orderText">sort</param>
        /// <returns></returns>
        public DataTable GetTable()
        {
            return GetDataTable(selectAllTerminal);
        }

        /// <summary>
        /// 获取所有配置表里数据。
        /// </summary>
        /// <returns></returns>
        public DataTable GetTable_Ref_Code(string textWhere, string orderText)
        {
            string query = string.Empty;
            if (!string.IsNullOrEmpty(textWhere))
                query += " where " + textWhere;
            if (!string.IsNullOrEmpty(orderText))
                query += " order by " + orderText;

            return GetDataTable(string.Format(selectAll_REF_CODE, query));
        }

        /// <summary>
        /// 获取所有配置表里数据。
        /// </summary>
        /// <returns></returns>
        public DataTable GetTable_ALL_Ports(string textWhere, string orderText)
        {
            string query = string.Empty;
            if (!string.IsNullOrEmpty(textWhere))
                query += " where " + textWhere;
            if (!string.IsNullOrEmpty(orderText))
                query += " order by " + orderText;

            return GetDataTable(string.Format(selectAllPorts, query));
        }

        /// <summary>
        /// Get DataTable
        /// </summary>
        /// <param name="sql">SQL statement</param>
        /// <returns></returns>
        private DataTable GetDataTable(string sql)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            DataTable dt = db.ExecuteDataTable(dbCommand);
            if (dt != null)
            {
                dt.TableName = "ST_Terminal";
            }
            return dt;
        }

      
    }
}
